package com.young.test.aop;

import java.util.Map;

import javax.inject.Named;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;

import com.young.test.domain.CachPoll;
import com.young.test.domain.Person;

@Order(2)
@Named
@Aspect
public class RoundAdvice {
	
	private static final Logger logger = Logger.getLogger(LogAspect.class);
	
	private Map<String, Person> personCach = CachPoll.getPersonCach();
	
	@Around("com.young.test.aop.SystemArchitecture.forAspectBeanOperation() && com.young.test.aop.SystemArchitecture.forAllPublicVoidMethod()")
	public void beforeInitExecution(ProceedingJoinPoint pJoinPoint) throws Throwable{
		logger.info("<======== Around begin ==========>");
		pJoinPoint.proceed();
		logger.info("<======== Around end ==========>");
	}
	/*
	@Around("com.young.test.aop.SystemArchitecture.withinDaoLayer() && com.young.test.aop.SystemArchitecture.forReturnPersonMethod()")
	public Person getPersonThrowCach(ProceedingJoinPoint pJoinPoint) throws Throwable{
		Object[] args = pJoinPoint.getArgs();
		String key = (String)args[0];
		if(personCach.containsKey(key)){
			logger.info("This Person has been cached");
			return personCach.get(key);
		}
		Person person = (Person)pJoinPoint.proceed();
		logger.info("This is new Person");
		return person;
	}
	//It is the same as the below 
	
	@Around("com.young.test.aop.SystemArchitecture.withinDaoLayer() && com.young.test.aop.SystemArchitecture.forReturnPersonMethod() && "+
			"args(name)")
	public Person getPersonThrowCach(ProceedingJoinPoint pJoinPoint,String name) throws Throwable{
		String key = name;
		if(personCach.containsKey(key)){
			logger.info("This Person has been cached");
			return personCach.get(key);
		}
		Person person = (Person)pJoinPoint.proceed();
		logger.info("This is new Person");
		return person;
	}
	
	//It is the same as the below 
	*/
	@Around("com.young.test.aop.SystemArchitecture.forReturnPersonByStringMethod(name)")
	public Person getPersonThrowCach(ProceedingJoinPoint pJoinPoint,String name) throws Throwable{
		String key = name;
		if(personCach.containsKey(key)){
			logger.info("This Person has been cached");
			return personCach.get(key);
		}
		Person person = (Person)pJoinPoint.proceed();
		logger.info("This is new Person");
		return person;
	}

}
